package com.book.common;

import com.book.dto.UserDto;
import com.book.redis.SessionProvider;
import com.book.util.Constants;
import com.book.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BaseController {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    @Autowired
    private SessionProvider sessionProvider;

    public void setSessionCookieId(UserDto userDto) {
        session = getSession();
        session.setAttribute("user",userDto);
        String csessionid = RequestUtils.getCSESSIONID(getRequest(), getResponse(),session);
        sessionProvider.setAttributeForUsername(csessionid, userDto);
    }


    public UserDto getCurrentUser() {
        String csessionid = RequestUtils.getCSESSIONID(getRequest(), getResponse(),getSession());
        UserDto attributeForUsername = sessionProvider.getAttributeForUsername(csessionid);
        assert attributeForUsername != null;
        return attributeForUsername;
    }

    public String getUserCode() {
        UserDto userDto = getCurrentUser();
        return userDto != null ? userDto.getUCode() : null;
    }

    public HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        this.request = ((ServletRequestAttributes) requestAttributes).getRequest();
        return request;
    }

    public HttpServletResponse getResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        this.response = ((ServletRequestAttributes) requestAttributes).getResponse();
        return response;
    }

    public HttpSession getSession() {
        this.session = getRequest().getSession();
        return session;
    }

    public Integer getCurrentId() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getId();
    }

    public String getCurrentName() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getUName();
    }

    public String getCurrentPhone() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getUPhone();
    }

    public String getCurrentEmail() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getUEmail();
    }

    public String getCuurentPass() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getUPass();
    }

    public void clearSession() {
        HttpSession session = getRequest().getSession();
        session.setAttribute(Constants.LOGIN_USER, null);
    }

    public void clearCookie() {
        Cookie cookie1 = new Cookie("account", null);
        Cookie cookie2 = new Cookie("pass", null);
        cookie1.setMaxAge(0);
        cookie2.setMaxAge(0);
    }
}
